/*
 * CommonResource.cpp
 *
 *  Created on: 2013. 9. 5.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "CommonResource.h"
#include "../NUtil/Utils.h"
#include "../engine.h"

namespace ProjectZ { namespace NResource {

CommonResource::CommonResource()
{
	pCommonAndroid	= new CommonData;
	pCommonIOS		= new CommonData;
}

CommonResource::~CommonResource()
{
	delete pCommonAndroid;
	delete pCommonIOS;
}

bool CommonResource::LoadResource()
{
	if (false == pCommonAndroid->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pCommonIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

CommonData::Flyweight * CommonResource::GetFly(User * pUser, CommonData::Key & clsKey)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pCommonAndroid->GetFly(clsKey);

	case NUtil::MarketType::IOS_KAKAO:
		return pCommonIOS->GetFly(clsKey);

	default:
		return NULL;
	}

	return NULL;
}

CommonData::~CommonData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void
CommonData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool CommonData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT NATIONAL_CODE, type, sub_type, value1, value2 FROM resource_common_config WHERE company=%d AND sale_cd=%d ORDER BY type, sub_type", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		CommonData::Flyweight * pFly = new CommonData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> pFly->_TYPE >> pFly->_SUB_TYPE >> pFly->_VALUE1 >> pFly->_VALUE2;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[CommonData::Key(pFly->_TYPE, pFly->_SUB_TYPE)] = pFly;
	}

	return true;
}

CommonData::Flyweight * CommonData::GetFly(CommonData::Key & clsKey)
{
	auto iter = clsFlyTree.find(clsKey);

	if( iter == clsFlyTree.end() )
		return NULL;
	else
		return iter->second;
}

int
CommonResource::GetLimitLevel(User * pUser)
{
	/*
	 * 현재 게임내 제한 레벨 조회
	 * @return :
	 * 		정상처리 : 게임내 제한 레벨 리턴
	 * 		오류발생 : -1 리턴
	 */
	CommonData * pCommonData = NULL;

	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		pCommonData = pCommonAndroid;
		break;

	case NUtil::MarketType::IOS_KAKAO:
		pCommonData = pCommonIOS;
		break;

	default:
		pCommonData = NULL;
	}

	if (NULL == pCommonData)
		return -1;

	CommonData::Key clsKey(1, 1);
	CommonData::Flyweight * pFly = pCommonData->GetFly(clsKey);
	if (NULL == pFly)
		return -1;

	return pFly->_VALUE1;
}

} /*NResource*/ } /* namespace ProjectZ */

